package edu.neepu;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainClass {
    String CHAPTER;
    @Test
    public void test3 () throws IOException {//此方法用来获取书籍名中文列表（用来创建书名）
        Document document = Jsoup.connect("http://www.shicimingju.com/book/index.html").timeout(50000).header("Accept-language", "zh-cn").get();
        Elements elements = document.select("h2");
        File file;//创建文件夹
        FileOutputStream stream = null;//new文件流
        for (Element element : elements) {
            Element a = element.select("a").first();
            String DIRECTORY=a.text() ;  //文件目录名



            try {
                file = new File("C:/Users/Administrator/Desktop/个人空间/workspace/小说/"+DIRECTORY+"/"+CHAPTER+".txt");

                if (!file.getParentFile().exists()&&!file.isDirectory()){
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                } else {
                    file.createNewFile();
                }
                byte[] contentInBytes = a.text().getBytes();//转化成字节形
                stream = new FileOutputStream(file);//将文件夹放在文件流中
                stream.write(contentInBytes);//写入
                stream.flush(); //写完之后刷新
                stream.close();//关闭流
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }



        }

    }

    @Test
    public void test4 () throws IOException {
        Document document = Jsoup.connect("http://www.shicimingju.com/book/index.html").timeout(50000).header("Accept-language", "zh-cn").get();
        Elements elements = document.select("h2");

        for (Element element : elements) {
            Element a = element.select("a").first();
            String URL = a.attr("abs:href");
            String DIRECTORY=a.text();


            Document document1 = Jsoup.connect(URL).timeout(50000).header("Accept-language", "zh-cn").get();
            Elements elements1 = document1.select(".book-mulu li");
            for (Element element1 : elements1) {
                Elements b = element1.select("a");

                    String URL1 = b.attr("abs:href");
                    if(URL1!=""){
                    //System.out.println(URL1);
                    // System.out.println(b.text());
                    String CHAPTER = b.text();
                    Document document3 = Jsoup.connect(URL1).timeout(50000).header("Accept-language", "zh-cn").get();
                    Elements elements3 = document3.select(".chapter_content");


                    for (Element element3 : elements3) {
                        Elements d = element3.select("p");
                        //System.out.println(d.text());


                        File file;//创建文件夹
                        FileOutputStream stream = null;//new文件流
                            if(URL1.indexOf("fengyuejian")<=0 || URL1.indexOf("(")<=0) {

                            if (CHAPTER.indexOf("?") <= 0) {


                                try {


                                    file = new File("C:/Users/Administrator/Desktop/个人空间/workspace/小说1/" + DIRECTORY + "/" + CHAPTER + ".txt");

                                    if (!file.getParentFile().exists() && !file.isDirectory()) {
                                        file.getParentFile().mkdirs();
                                        file.createNewFile();
                                    } else {
                                        file.createNewFile();
                                    }
                                    byte[] contentInBytes = d.text().getBytes();//转化成字节形
                                    stream = new FileOutputStream(file);//将文件夹放在文件流中
                                    stream.write(contentInBytes);//写入
                                    stream.flush(); //写完之后刷新
                                    stream.close();//关闭流
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                    }


                }else{
                        continue;
                    }

            }



        }


    }





    @Test
    public void test5 () throws IOException{
        Document document1 = Jsoup.connect("http://www.shicimingju.com/book/xiyouji.html").header("Accept-language", "zh-cn").get();
        Elements elements1 = document1.select(".book-mulu");
        for (Element element1 : elements1){
            Elements b = element1.select("li");
            System.out.println(b.text());

        }

    }
    @Test
    public void test6 () throws IOException{
        Document document2 = Jsoup.connect("http://www.shicimingju.com/book/index.html").header("Accept-language", "zh-cn").get();
        Elements elements2 = document2.select("h2");
        for (Element element2 : elements2){
            Elements c = element2.select("a");
            System.out.println(c.text());

        }

    }
    public void test7 () throws IOException{
        Document document2 = Jsoup.connect("http://www.shicimingju.com/book/index.html").header("Accept-language", "zh-cn").get();

    }

}




